package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.UserPointBean;
import com.cljz.makepassenger.present.activity.PointP;
import com.cljz.makepassenger.ui.adapter.PointAdapter;
import com.cljz.makepassenger.utils.L;
import com.jakewharton.rxbinding.view.RxView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

public class PointActivity extends BaseActivity<PointP> {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.iv_title_right1)
    ImageView ivTitleRight1;
    @BindView(R.id.fl_title_right1)
    FrameLayout flTitleRight1;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_totalpoints)
    TextView tvTotalpoints;
    @BindView(R.id.btn_exchange)
    Button btnExchange;
    @BindView(R.id.rv_mypoint_list)
    XRecyclerView rvMypointList;
    private PointAdapter pointAdapter;

    private int page = 1;
    private boolean isRefresh = false;
    private boolean isLoadMore = false;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("我的积分");
        ivTitleRight.setImageResource(R.mipmap.point_help);

        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvMypointList.setLayoutManager(manager);
        pointAdapter = new PointAdapter(this);
        rvMypointList.setAdapter(pointAdapter);
        rvMypointList.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        rvMypointList.setPullRefreshEnabled(true);
        rvMypointList.setLoadingMoreEnabled(true);
        rvMypointList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                isRefresh = true;
                getP().getPointList(page);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                getP().getPointList(page);
            }
        });
        rvMypointList.refresh();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_mypoints;
    }

    @Override
    public PointP newP() {
        return new PointP();
    }

    @Override
    protected void initListener() {
        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });
        RxView.clicks(btnExchange)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent(PointActivity.this).putString("myPoints", tvTotalpoints.getText().toString().trim()).to(ExchangeActivity.class).launch();
                    }
                });
        RxView.clicks(flTitleRight)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent(PointActivity.this).to(HelpCenterActivity.class).launch();
                    }
                });
    }

    public void LoadDataSuccess(UserPointBean.DataBean data) {
        tvTotalpoints.setText(data.getTotalPoints());
//        btnExchange.setEnabled(Integer.parseInt(data.getTotalPoints()) > 100);
        L.e("1_net:" + data.getPointsList().size());
        if (isLoadMore) {
            if (data.getPointsList().size() == 0) {
                rvMypointList.setNoMore(true);
            } else {
                rvMypointList.setNoMore(false);
                pointAdapter.addData(data.getPointsList(), isRefresh);
            }
            rvMypointList.loadMoreComplete();
            isLoadMore = false;
        } else {
            pointAdapter.addData(data.getPointsList(), isRefresh);
            isRefresh = false;
            rvMypointList.refreshComplete();
            rvMypointList.setNoMore(false);
        }
    }

    public void LoadDataError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
        rvMypointList.refreshComplete();
    }


}
